package com.xt.base.model.base;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Calendar;

import com.xt.base.dao.hibernate.code.IAutoCreateCode;
import com.xt.base.exception.SystemError;
import com.xt.base.pojo.BasePOJO;
import com.xt.base.util.Compressor;

/**
 * 错误PO模型 <br> *
 * 
 * <p>
 * <a href="ErrorPO.java.html"><i>查看源文件</i></a>
 * </p>
 * 
 * @author 杨洪波
 * @version $Revision: $<br>
 *          $Id: $
 * @hibernate.class table="Errors"  batch-size="200"
 * @hibernate.discriminator column="Type" type="string" length="50" lazy="false"
 */
public abstract class ErrorPO extends BasePOJO implements
		IAutoCreateCode {

	private static final long serialVersionUID = -9140734729782192142L;

	/** 错误对象系列化值 * */
	private byte[] errorObject;
	
	private Calendar time;
	
	public ErrorPO(){}

	public ErrorPO(SystemError err) {
		time = Calendar.getInstance();
		try {
			ByteArrayOutputStream buffer = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(buffer);
			oos.writeObject(err);
			oos.flush();
			buffer.flush();
			errorObject = Compressor.zipArray(buffer.toByteArray());
			oos.close();
			buffer.close();
		} catch(Throwable thr){
			throw new SystemError(err, new Object[]{thr});
		}
	}

	public String[] getUnpaseProperties() {
		return new String[] { "errorObject" };
	}

	/**
	 * @return 错误对象
	 *  */
	public SystemError getError() {
		try {
			ObjectInputStream ois = new ObjectInputStream(
					new ByteArrayInputStream(Compressor.unzipArray(getErrorObject())));
			return (SystemError)ois.readObject();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @hibernate.property type="com.xt.base.dao.hibernate.userType.BinaryBlobType" column="ErrorObject"
	 */
	public byte[] getErrorObject() {
		return errorObject;
	}

	public void setErrorObject(byte[] errorObject) {
		this.errorObject = errorObject;
	}

	/**
	 * @hibernate.property column="Time"
	 */
	public Calendar getTime() {
		return time;
	}

	public void setTime(Calendar time) {
		this.time = time;
	}

}
